package com.hs.sfzy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hs.sfzy.entity.Course;
import com.hs.sfzy.entity.Question;
import com.hs.sfzy.entity.User;
import com.hs.sfzy.mapper.CourseMapper;
import com.hs.sfzy.mapper.QuestionMapper;
import com.hs.sfzy.mapper.ReplyMapper;
import com.hs.sfzy.mapper.UserMapper;
import com.hs.sfzy.service.QuestionService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @BelongsProject: sfzyBackEnd
 * @BelongsPackage: com.hs.sfzy.service.impl
 * @ClassName QuestionServiceImpl
 * @Author: 陈千鱼不是鱼
 * @CreateTime: 2024-10-24  17:51
 * @Description: TODO
 * @Version: 1.0
 */
@Service
@RequiredArgsConstructor
public class QuestionServiceImpl implements QuestionService {

    private final QuestionMapper questionMapper;
    private final ReplyMapper replyMapper;
    private final UserMapper userMapper;

    @Override
    public PageInfo<Question> page(Integer pageNum, Integer pageSize, String title) {
        PageHelper.startPage(pageNum,pageSize);
        List<Question> list = questionMapper.selectAll(title);
        for (Question question : list) {
            question.setUser(userMapper.getUserById(question.getUserId()));
        }
        PageInfo<Question> pageInfo = new PageInfo<Question>(list);
        return pageInfo;
    }

    @Override
    public PageInfo<Question> pageForWx(Integer pageNum, Integer pageSize, Question question) {
        List<Question> list = questionMapper.selectAllForWx(question);
        list.forEach(q -> {
            questionMapper.updateReplyCount(q.getId());
            questionMapper.updatelastReplyTime(q.getId());
        });
        PageHelper.startPage(pageNum,pageSize);
        list = questionMapper.selectAllForWx(question);
        for (Question ques : list) {
            ques.setUser(userMapper.getUserById(ques.getUserId()));
        }
        PageInfo<Question> pageInfo = new PageInfo<Question>(list);
        return pageInfo;
    }

    @Override
    public void save(Question question) {
        question.setCreateTime(new Date());
        question.setLastReplyTime(null);
        int i =  questionMapper.save(question);
        if (i == 0){
            throw new RuntimeException("添加失败");
        }
    }

    @Override
    public void deleteById(Integer id) {
//        先根据问题id获取绑定改id的回复表数据的id
        List<Integer> replyIds = replyMapper.getIdByQuestionId(id);
//        判断ids是否为空
        if (!replyIds.isEmpty()){
//            删除所有的关联该问题id的恢复
            replyMapper.deleteBeach(replyIds);
        }
//      保证不存在恢复的存在下删除问题表
        int i = questionMapper.deleteById(id);
        if (i == 0){
            throw new RuntimeException("删除失败");
        }
    }

    @Override
    public void update(Question question) {
        int i = questionMapper.update(question);
        if (i == 0){
            throw new RuntimeException("更新失败");
        }
    }

    @Override
    public List<Question> findAllIDAndTitle() {
        return questionMapper.findAllIDAndTitle();
    }
}
